package com.carkeeper.controller;


import com.carkeeper.dto.CompTypeDTO;
import com.carkeeper.pojo.CompTypePO;
import com.carkeeper.service.CompTypeService;
import com.carkeeper.vo.Pager;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/componentType")
public class CompTypeController {

    private final CompTypeService compTypeService;


    public CompTypeController(CompTypeService compTypeService) {
        this.compTypeService = compTypeService;
    }

    /*
     * 获取配件分类详情
     * @author xxj
     * @date 2020/5/27-17:36
     * @param id
     * @return org.springframework.http.ResponseEntity<com.carkeeper.pojo.CompTypePO>
     */
    @GetMapping("/{id}")
    public CompTypeDTO queryCompTypeById(@PathVariable("id") Long id){
        return compTypeService.queryCompTypeById(id);
    }

    /*
     * 获取配件分类分页数据
     * @author xxj
     * @date 2020/5/28-16:46
     * @param page
     * @param pagesize
     * @param parentId
     * @param name
     * @param code
     * @return org.springframework.http.ResponseEntity<com.carkeeper.dto.PageDTO<com.carkeeper.dto.CompTypeDTO>>
     */
    @GetMapping
    public Pager<CompTypeDTO> getFirstCompTypeByPage(
            @RequestParam(value = "page",defaultValue = "1") Long page,
            @RequestParam(value = "pageSize",defaultValue = "10")Long pageSize,
            @RequestParam(value = "parentId",required = false)Long parentId,
            @RequestParam(value = "name",required = false)String name,
            @RequestParam(value = "code",required = false)String code) {
        return compTypeService.getFirstCompTypeByPage(page, pageSize, parentId, name, code);
    }
    /*
     * 更新配件分类
     * @author xxj
     * @date 2020/5/28-17:48
     * @param id
     * @param compTypeDto
     * @return org.springframework.http.ResponseEntity<java.lang.String>
     */
    @PutMapping("/{id}")
    public String updateFirstCompType(@PathVariable("id") Long id,@RequestBody CompTypePO compTypePO) {
        return compTypeService.updateFirstCompType(id, compTypePO);
    }
    /*
     * 新增一级配件分类
     * @author xxj
     * @date 2020/5/30-18:42
     * @param compTypeDto
     * @return java.lang.String
     */
    @PostMapping
    public String addFirstCompType(
            @RequestBody CompTypePO compTypePO) {
        return compTypeService.addFirstCompType(compTypePO);
    }
    /*
     * 根据ID删除一级配件分类
     * @author xxj
     * @date 2020/5/30-18:24
     * @param id
     * @return java.lang.String
     */
    @DeleteMapping("/{id}")
    public String deleteFirstCompTypeById(@PathVariable("id") Long id){
        return compTypeService.deleteFirstCompTypeById(id);
    }

}
